# Libraries
library(ggplot2)
library(dplyr)
library(tidyr)
library(viridis)
library(hrbrthemes)
library(ggpubr)

df <- read.csv("data_with_media_measures.csv")
table(df$partisanship2)
df$partisanship3[df$partisanship2 < 4] <- "Democrat"
df$partisanship3[df$partisanship2 == 4] <- "Independent"
df$partisanship3[df$partisanship2 > 4] <- "Republican"
table(df$partisanship3)

df <- df[!is.na(df$partisanship3),]
df$partisanship3 <- factor(df$partisanship3, levels = c("Republican", "Independent", "Democrat"))



# The diamonds dataset is natively available with R.

# With transparency (right)

p1 <- ggplot(data=df, aes(x=partisan_news_flow, group=partisanship3, fill=partisanship3)) +
  geom_density(adjust=1.5, alpha=.4) + 
  xlab("(1-1) News use slant (left to right)") +
  ylab("") + 
  theme_light() + 
  scale_fill_manual(values = c("gray20", "gray60", "gray90")) + # Grayscale fill
  scale_color_manual(values = c("gray20", "gray60", "gray90")) + # Grayscale line color
  guides(fill = guide_legend(title = "Partisanship"))
  
p1
p1 <- p1 + geom_vline(xintercept = 0) + 
  theme(text = element_text(size = 20),axis.text.y=element_blank(),
                 axis.ticks.y=element_blank())  
p1


p3 <- ggplot(data=df, aes(x=outlet_diversity, group=partisanship3, fill=partisanship3)) +
  geom_density(adjust=1.5, alpha=.4) +
  theme_bw() + 
  xlab("(1-2) News use diversity") +
  ylab("") + 
  theme_light() + 
  scale_fill_manual(values = c("gray20", "gray60", "gray90")) + # Grayscale fill
  scale_color_manual(values = c("gray20", "gray60", "gray90")) + # Grayscale line color
  guides(fill = guide_legend(title = "Partisanship"))

p3 <- p3 + 
  theme(text = element_text(size = 20),axis.text.y=element_blank(),
        axis.ticks.y=element_blank())   
p3


p5 <- ggplot(data=df, aes(x=far_right, group=partisanship3, fill=partisanship3)) +
  geom_density(adjust=1.5, alpha=.4) +
  theme_bw() + 
  xlab("(1-3) Right extremity") +
  ylab("") + 
  theme_light() + 
  scale_fill_manual(values = c("gray20", "gray60", "gray90")) + # Grayscale fill
  scale_color_manual(values = c("gray20", "gray60", "gray90")) + # Grayscale line color
  guides(fill = guide_legend(title = "Partisanship"))

p5 <- p5 + 
  theme(text = element_text(size = 20),axis.text.y=element_blank(),
        axis.ticks.y=element_blank())   
 


p6 <- ggplot(data=df, aes(x=far_left, group=partisanship3, fill=partisanship3)) +
  geom_density(adjust=1.5, alpha=.4) +
  theme_bw() + 
  xlab("(1-4) Left extremity") +
  ylab("") + 
  theme_light() + 
  scale_fill_manual(values = c("gray20", "gray60", "gray90")) + # Grayscale fill
  scale_color_manual(values = c("gray20", "gray60", "gray90")) + # Grayscale line color
  guides(fill = guide_legend(title = "Partisanship"))

p6 <- p6 + 
  theme(text = element_text(size = 20),axis.text.y=element_blank(),
        axis.ticks.y=element_blank())   
p6


ggarrange(p1, p3, p5, p6, ncol=2, nrow=2, common.legend = TRUE, legend="bottom")


